package com.aube.interact.taste.player;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aube.interact.taste.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by hill on 10/24.
 */
public class ResolutionSelector extends LinearLayout implements View.OnClickListener {

    private LinearLayout mRoot;
    private LinearLayout mResolutionGroup;
    private Context mContext;

    private static final int COLOR_NORMAL = Color.WHITE;
    private static int COLOR_SELECTED = Color.WHITE;

    private int margin = 0;
    private List<ResolutionOption> mSelectors;
    private int mCurrent = 0;

    private ISelectorListener iSelector;

    public ResolutionSelector(Context context) {
        this(context, null);
    }
    public ResolutionSelector(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public ResolutionSelector(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init();
    }
    private void init() {
        mRoot = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.aube_intera_layout_resolution, this);

        mResolutionGroup = (LinearLayout) mRoot.findViewById(R.id.resolution_group);

        margin = getResources().getDimensionPixelOffset(R.dimen.resolution_margin);
        COLOR_SELECTED = getResources().getColor(R.color.video_quanlity_selected_color);
    }

    public void fill(Map<Integer, String> maps, int code, ISelectorListener listener) {
        if(maps == null || listener == null)
            return;
        Iterator<Map.Entry<Integer, String>> iterator = maps.entrySet().iterator();
        List<ResolutionOption> list = new ArrayList<>();
        while (iterator.hasNext()) {
            Map.Entry<Integer, String> entry = iterator.next();
            ResolutionOption option = new ResolutionOption(entry.getValue(), entry.getKey());
            list.add(option);
        }

        Collections.sort(list, new Comparator<ResolutionOption>() {
            @Override
            public int compare(ResolutionOption lhs, ResolutionOption rhs) {
                if(lhs.type < rhs.type)
                    return -1;
                else if(lhs.type > rhs.type)
                    return 1;
                return 0;
            }
        });

        int i=0;
        int current=-1;
        for(ResolutionOption option : list) {
            if(code == option.type) {
                current = i;
            }
            i++;
        }

        fill(list, current, listener);
    }

    public void fill(List<ResolutionOption> data, int current, ISelectorListener listener) {
        if(data == null || current < 0 || current >= data.size())
            return;
        setVisibility(View.GONE);
        this.iSelector = listener;

        this.mSelectors = data;

        mResolutionGroup.removeAllViews();

        int index = 0;
        for(ResolutionOption item : data) {
            TextView mTxt = (TextView) LayoutInflater.from(mContext).inflate(R.layout.aube_intera_resolution_item, null);
            LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.CENTER;
            params.setMargins(0, margin, 0, 0);
            mResolutionGroup.addView(mTxt, params);

            mTxt.setText(item.des);
            mTxt.setTag(R.id.resolution_id, index);
            mTxt.setOnClickListener(this);

            if(index == current) {
                mTxt.setTextColor(COLOR_SELECTED);
                mCurrent = index;
            } else
                mTxt.setTextColor(COLOR_NORMAL);

            index++;
        }

        mRoot.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(iSelector != null)
                    iSelector.dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = (int) v.getTag(R.id.resolution_id);
        //do something
        ResolutionOption option = mSelectors.get(id);

        ((TextView) mResolutionGroup.getChildAt(mCurrent)).setTextColor(COLOR_NORMAL);
        ((TextView) mResolutionGroup.getChildAt(id)).setTextColor(COLOR_SELECTED);

        mCurrent = id;

        if(iSelector == null)
            return;

        iSelector.onSelect(option.des, option.type);
    }

    public static class ResolutionOption {
        public String des;
        public int type;
        public ResolutionOption(String des, int type) {
            this.des = des;
            this.type = type;
        }
    }

    public interface ISelectorListener {
        public void onSelect(String des, int type);
        public void dismiss();
    }

}
